package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Set;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllUsers", query = "select myUser from User myUser"),
		@NamedQuery(name = "findUserByName", query = "select myUser from User myUser where myUser.name = ?1"),
		@NamedQuery(name = "findUserByNameContaining", query = "select myUser from User myUser where myUser.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByUsername", query = "select myUser from User myUser where myUser.username = ?1"),
		@NamedQuery(name = "findUserByUsernameContaining", query = "select myUser from User myUser where myUser.username like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByPassword", query = "select myUser from User myUser where myUser.password = ?1"),
		@NamedQuery(name = "findUserByPasswordContaining", query = "select myUser from User myUser where myUser.password like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByFirstname", query = "select myUser from User myUser where myUser.firstname = ?1"),
		@NamedQuery(name = "findUserByFirstnameContaining", query = "select myUser from User myUser where myUser.firstname like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByLastname", query = "select myUser from User myUser where myUser.lastname = ?1"),
		@NamedQuery(name = "findUserByLastnameContaining", query = "select myUser from User myUser where myUser.lastname like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByGender", query = "select myUser from User myUser where myUser.gender = ?1"),
		@NamedQuery(name = "findUserByGenderContaining", query = "select myUser from User myUser where myUser.gender like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByBirthday", query = "select myUser from User myUser where myUser.birthday = ?1"),
		@NamedQuery(name = "findUserByCareer", query = "select myUser from User myUser where myUser.career = ?1"),
		@NamedQuery(name = "findUserByCareerContaining", query = "select myUser from User myUser where myUser.career like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByEmail", query = "select myUser from User myUser where myUser.email = ?1"),
		@NamedQuery(name = "findUserByEmailContaining", query = "select myUser from User myUser where myUser.email like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByPhone", query = "select myUser from User myUser where myUser.phone = ?1"),
		@NamedQuery(name = "findUserByPhoneContaining", query = "select myUser from User myUser where myUser.phone like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByCellphone", query = "select myUser from User myUser where myUser.cellphone = ?1"),
		@NamedQuery(name = "findUserByCellphoneContaining", query = "select myUser from User myUser where myUser.cellphone like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByQq", query = "select myUser from User myUser where myUser.qq = ?1"),
		@NamedQuery(name = "findUserByQqContaining", query = "select myUser from User myUser where myUser.qq like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByWechat", query = "select myUser from User myUser where myUser.wechat = ?1"),
		@NamedQuery(name = "findUserByWechatContaining", query = "select myUser from User myUser where myUser.wechat like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserByEnabled", query = "select myUser from User myUser where myUser.enabled = ?1"),
		@NamedQuery(name = "findUserByDescription", query = "select myUser from User myUser where myUser.description = ?1"),
		@NamedQuery(name = "findUserByDescriptionContaining", query = "select myUser from User myUser where myUser.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findUserById", query = "select myUser from User myUser where myUser.id = ?1"),
		@NamedQuery(name = "findUserByPrimaryKey", query = "select myUser from User myUser where myUser.id = ?1 "),
		@NamedQuery(name = "deleteUsersByIds", query = "delete User where id in (?1)"),
		@NamedQuery(name = "findUsersByIds", query = "select myUser from User myUser where myUser.id in (?1)"),
		@NamedQuery(name = "findUserByPhyscialPrimaryKey", query = "select myUser from User myUser where  myUser.name = ?1 and myUser.birthday = ?2 and myUser.email = ?3"),
		@NamedQuery(name = "countUsersByName", query = "select count(myUser)  from User myUser where myUser.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "User")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class User implements Serializable, Comparable<User> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "username")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String username;

	@Column(name = "password")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String password;

	@Column(name = "firstname")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String firstname;

	@Column(name = "lastname")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String lastname;

	@Column(name = "gender")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String gender;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "birthday")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar birthday;

	@Column(name = "career")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String career;

	@Column(name = "email")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String email;

	@Column(name = "phone")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String phone;

	@Column(name = "cellphone")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String cellphone;

	@Column(name = "qq")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String qq;

	@Column(name = "wechat")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String wechat;

	@Column(name = "enabled")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Boolean enabled;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "detector", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detect> detectsForDetector;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "auditor", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Event> eventsForAuditor;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "reportor", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Event> eventsForReportor;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "author", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Url> urlsForAuthor;
	@JSONField(serialize = false)
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(catalog = "ema", name = "user2authority", joinColumns = {
			@JoinColumn(name = "user", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "authority", referencedColumnName = "id") })
	@XmlElement(name = "", namespace = "")
	Set<Authority> authoritysForUser;
	@JSONField(serialize = false)
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(catalog = "ema", name = "user2detectdepartment", joinColumns = {
			@JoinColumn(name = "user", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "detectdepartment", referencedColumnName = "id") })
	@XmlElement(name = "", namespace = "")
	Set<Detectdepartment> detectdepartmentsForUser;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return this.username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return this.password;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getFirstname() {
		return this.firstname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getLastname() {
		return this.lastname;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getGender() {
		return this.gender;
	}

	public void setBirthday(Calendar birthday) {
		this.birthday = birthday;
	}

	public Calendar getBirthday() {
		return this.birthday;
	}

	public void setCareer(String career) {
		this.career = career;
	}

	public String getCareer() {
		return this.career;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return this.email;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPhone() {
		return this.phone;
	}

	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}

	public String getCellphone() {
		return this.cellphone;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getQq() {
		return this.qq;
	}

	public void setWechat(String wechat) {
		this.wechat = wechat;
	}

	public String getWechat() {
		return this.wechat;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public Boolean getEnabled() {
		return this.enabled;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDetectsForDetector(Set<Detect> detectsForDetector) {
		this.detectsForDetector = detectsForDetector;
	}

	public Set<Detect> getDetectsForDetector() {
		if (detectsForDetector == null) {
			detectsForDetector = new java.util.LinkedHashSet<Detect>();
		}
		return detectsForDetector;
	}

	public void setEventsForAuditor(Set<Event> eventsForAuditor) {
		this.eventsForAuditor = eventsForAuditor;
	}

	public Set<Event> getEventsForAuditor() {
		if (eventsForAuditor == null) {
			eventsForAuditor = new java.util.LinkedHashSet<Event>();
		}
		return eventsForAuditor;
	}

	public void setEventsForReportor(Set<Event> eventsForReportor) {
		this.eventsForReportor = eventsForReportor;
	}

	public Set<Event> getEventsForReportor() {
		if (eventsForReportor == null) {
			eventsForReportor = new java.util.LinkedHashSet<Event>();
		}
		return eventsForReportor;
	}

	public void setUrlsForAuthor(Set<Url> urlsForAuthor) {
		this.urlsForAuthor = urlsForAuthor;
	}

	public Set<Url> getUrlsForAuthor() {
		if (urlsForAuthor == null) {
			urlsForAuthor = new java.util.LinkedHashSet<Url>();
		}
		return urlsForAuthor;
	}

	public void setAuthoritysForUser(Set<Authority> authoritysForUser) {
		this.authoritysForUser = authoritysForUser;
	}

	public Set<Authority> getAuthoritysForUser() {
		if (authoritysForUser == null) {
			authoritysForUser = new java.util.LinkedHashSet<Authority>();
		}
		return authoritysForUser;
	}

	public void setDetectdepartmentsForUser(Set<Detectdepartment> detectdepartmentsForUser) {
		this.detectdepartmentsForUser = detectdepartmentsForUser;
	}

	public Set<Detectdepartment> getDetectdepartmentsForUser() {
		if (detectdepartmentsForUser == null) {
			detectdepartmentsForUser = new java.util.LinkedHashSet<Detectdepartment>();
		}
		return detectdepartmentsForUser;
	}

	public void copy(User that) {

		setId(that.getId());

		setName(that.getName());
		setUsername(that.getUsername());
		setPassword(that.getPassword());
		setFirstname(that.getFirstname());
		setLastname(that.getLastname());
		setGender(that.getGender());
		setBirthday(that.getBirthday());
		setCareer(that.getCareer());
		setEmail(that.getEmail());
		setPhone(that.getPhone());
		setCellphone(that.getCellphone());
		setQq(that.getQq());
		setWechat(that.getWechat());
		setEnabled(that.getEnabled());
		setDescription(that.getDescription());

		setDetectsForDetector(
				new java.util.LinkedHashSet<Detect>(that.getDetectsForDetector()));

		setEventsForAuditor(new java.util.LinkedHashSet<Event>(that.getEventsForAuditor()));

		setEventsForReportor(
				new java.util.LinkedHashSet<Event>(that.getEventsForReportor()));

		setUrlsForAuthor(new java.util.LinkedHashSet<Url>(that.getUrlsForAuthor()));

		setAuthoritysForUser(
				new java.util.LinkedHashSet<Authority>(that.getAuthoritysForUser()));

		setDetectdepartmentsForUser(new java.util.LinkedHashSet<Detectdepartment>(
				that.getDetectdepartmentsForUser()));

	}

	public User() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("username=[").append(username).append("] ");
		buffer.append("password=[").append(password).append("] ");
		buffer.append("firstname=[").append(firstname).append("] ");
		buffer.append("lastname=[").append(lastname).append("] ");
		buffer.append("gender=[").append(gender).append("] ");
		buffer.append("birthday=[").append(birthday).append("] ");
		buffer.append("career=[").append(career).append("] ");
		buffer.append("email=[").append(email).append("] ");
		buffer.append("phone=[").append(phone).append("] ");
		buffer.append("cellphone=[").append(cellphone).append("] ");
		buffer.append("qq=[").append(qq).append("] ");
		buffer.append("wechat=[").append(wechat).append("] ");
		buffer.append("enabled=[").append(enabled).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof User))
			return false;
		User comparedUser = (User) obj;
		if (!CheckPropertyEqual(this.name, comparedUser.name)) {
			return false;
		}
		if (!CheckPropertyEqual(this.birthday, comparedUser.birthday)) {
			return false;
		}
		if (!CheckPropertyEqual(this.email, comparedUser.email)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(User o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
